package cn.shop.dys.fragment;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.google.gson.reflect.TypeToken;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.lidroid.xutils.view.annotation.ViewInject;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import cn.shop.dys.R;
import cn.shop.dys.adapter.CommonIndentAdapter;
import cn.shop.dys.bean.AllIndentItem;
import cn.shop.dys.bean.CartItem;
import cn.shop.dys.bean.MyCheckoutAddup;
import cn.shop.dys.bean.OrderInfo;
import cn.shop.dys.constant.CommonConstant;
import cn.shop.dys.constant.SPConstant;
import cn.shop.dys.util.SPUtil;
import cn.shop.lib_common.CommonUtil;
import de.greenrobot.event.EventBus;

/**
 * 作者：jian on 2016/4/21 23:38
 * 孙建
 */
public abstract class IndentBaseFragment extends BaseFragment {
    @Override
    public View createView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_all_indent, null);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        init();
     //   Log.d("test", "数据" + mAllIndentItems.size());

    }

    public void init() {
        initEvent();
        initListView();
    }

    public void initListView() {
       initAllOrderInfo("0", "1");
      initAllOrderInfo("0", "2");
        initAllOrderInfo("0", "3");
    }

    public void initEvent() {

    }

    public List<OrderInfo> mOrderInfos = new ArrayList<>();
    public List<AllIndentItem> mAllIndentItems = new ArrayList<>();

    private void initAllOrderInfo(final String page, final String type) {
        mHttpUtils.configDefaultHttpCacheExpiry(0);
        mHttpUtils.configTimeout(10000);
        RequestParams params = new RequestParams();
        params.addHeader("userid", SPUtil.getString(mContext, SPConstant.USER_INFO_USER_ID));
        params.addBodyParameter("type", type);
        params.addBodyParameter("page", page);
        params.addBodyParameter("pageNum", "10");
        mHttpUtils.send(HttpRequest.HttpMethod.POST, CommonConstant.HOST_ORDER_LIST, params,
                new RequestCallBack<String>() {
                    @Override
                    public void onSuccess(ResponseInfo<String> responseInfo) {
                        int statusCode = responseInfo.statusCode;
                        if (statusCode == 200) {
                            String result = responseInfo.result;
                            try {
                                JSONObject jsonObject = new JSONObject(result);
                                String response = jsonObject.getString("response");
                                if (response.equals("orderList")) {
                                    String orderList = jsonObject.getString("orderList");
                                    List<OrderInfo> orderInfos = mGson.fromJson(orderList,
                                            new TypeToken<List<OrderInfo>>() {
                                            }.getType());
                                    if (orderInfos.size() > 0) {
                                        mOrderInfos.addAll(orderInfos);
                                        for (OrderInfo orderInfo : orderInfos) {
                                          //  Log.d("test",orderInfo.getOrderId());
                                            initAllDetailOrderInfo(orderInfo.getOrderId());
                                        }
                                    }
                                } else {
                                    String error = jsonObject.getString("error");
                                    String error_code = jsonObject.getString(CommonConstant.ERROR_CODE);
                                    if (error_code.equals(CommonConstant.ERROR_CODE_NO_USERID_ON_REQUEST_HEAD)) {
                                        CommonUtil.showToast(mContext, error);
                                    } else if (error_code.equals(CommonConstant.ERROR_CODE_UNLOGIN)) {
                                        CommonUtil.showToast(mContext, error);
                                    }
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        } else {
                            CommonUtil.showToast(mContext, "请求失败:错误码为" + statusCode);
                        }
                    }

                    @Override
                    public void onFailure(HttpException error, String msg) {
                   //     CommonUtil.showToast(mContext, "网络未连接2" + msg);
                    }

                });
    }

    private void initAllDetailOrderInfo(String orderId) {
        mHttpUtils.configDefaultHttpCacheExpiry(0);
        mHttpUtils.configTimeout(10000);
        RequestParams params = new RequestParams();
        params.addHeader("userid", SPUtil.getString(mContext, SPConstant.USER_INFO_USER_ID));
        params.addBodyParameter("orderId",orderId);
        //params.addQueryStringParameter("orderId",orderId);
        mHttpUtils.send(HttpRequest.HttpMethod.POST, CommonConstant.HOST_ORDER_DETAIL, params,
                new RequestCallBack<String>() {
                    @Override
                    public void onSuccess(ResponseInfo<String> responseInfo) {
                        int statusCode = responseInfo.statusCode;
                        Log.d("test","statusCode"+statusCode);
                        if (statusCode == 200) {
                            String result = responseInfo.result;
                            try {
                                JSONObject jsonObject = new JSONObject(result);
                                String response = jsonObject.getString("response");
                                Log.d("test",response);
                                if (response.equals("orderDetail")) {
                                    String orderInfoStr = jsonObject.getString("orderInfo");
                                    OrderInfo orderInfo = mGson.fromJson(orderInfoStr, OrderInfo.class);
                                    String productListStr = jsonObject.getString("productList");
                                    List<CartItem> cartItems = mGson.fromJson(productListStr,
                                            new TypeToken<List<CartItem>>() {
                                            }.getType());
                                    Log.d("test","列表"+cartItems.toString());
                                    String checkoutAddup = jsonObject.getString("checkoutAddup");
                                    MyCheckoutAddup myCheckoutAddup = mGson.fromJson(checkoutAddup, MyCheckoutAddup.class);
                                    mAllIndentItems.add(new AllIndentItem(orderInfo, cartItems, myCheckoutAddup));
                                    Log.d("test","前"+mAllIndentItems.toString());
                                   /* if (mOrderInfos.size() % 10 == 0) {
                                        int myPage = Integer.parseInt(page) + 1;
                                        initAllOrderInfo(String.valueOf(myPage), type);
                                    } else {*/
                                    if (getStatus() == CommonConstant.INDENT_TYPE_NOT_EVALUATE) {
                                        for (AllIndentItem mAllIndentItem : mAllIndentItems) {
                                            OrderInfo tOrderInfo = mAllIndentItem.getmOrderInfo();
                                       //     Log.d("test", "flag" + tOrderInfo.getFlag());
                                            if (tOrderInfo.getFlag() == 1 || tOrderInfo.getFlag() == 2) {
                                                mAllIndentItems.remove(mAllIndentItem);
                                            }
                                        }
                                    } else if (getStatus() == CommonConstant.INDENT_TYPE_NOT_PAY) {
                                        for (AllIndentItem mAllIndentItem : mAllIndentItems) {
                                            OrderInfo tOrderInfo = mAllIndentItem.getmOrderInfo();
                                            if (tOrderInfo.getFlag() == 2 || tOrderInfo.getFlag() == 3) {
                                                mAllIndentItems.remove(mAllIndentItem);
                                            }
                                        }
                                    } else if (getStatus() == CommonConstant.INDENT_TYPE_NOT_RECEIVE_GOODS) {
                                        for (AllIndentItem mAllIndentItem : mAllIndentItems) {
                                            OrderInfo tOrderInfo = mAllIndentItem.getmOrderInfo();
                                            if (tOrderInfo.getFlag() == 1 || tOrderInfo.getFlag() == 3) {
                                                mAllIndentItems.remove(mAllIndentItem);
                                            }
                                        }
                                    }
                                    Log.d("test","后"+mAllIndentItems.toString());
                                    setAdapter();
                                    //  }
                                } else {
                                    String error = jsonObject.getString("error");
                                    String error_code = jsonObject.getString(CommonConstant.ERROR_CODE);
                                    if (error_code.equals(CommonConstant.ERROR_CODE_NO_USERID_ON_REQUEST_HEAD)) {
                                        CommonUtil.showToast(mContext, error);
                                    } else if (error_code.equals(CommonConstant.ERROR_CODE_UNLOGIN)) {
                                        CommonUtil.showToast(mContext, error);
                                    }
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        } else {
                            CommonUtil.showToast(mContext, "请求失败:错误码为" + statusCode);
                        }
                    }

                    @Override
                    public void onFailure(HttpException error, String msg) {
                   //     CommonUtil.showToast(mContext, "网络未连接1" + msg);
                    }

                });
    }

    public abstract int getStatus();

    public void setAdapter() {
    }
}
